package com.elder.web.controller.business;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.elder.business.domain.MedicationRecords;
import com.elder.business.domain.MedicationRecordsDTO;
import com.elder.business.service.IMedicationRecordsService;
import com.elder.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.elder.common.annotation.Log;
import com.elder.common.core.controller.BaseController;
import com.elder.common.core.domain.AjaxResult;
import com.elder.common.enums.BusinessType;
import com.elder.common.utils.poi.ExcelUtil;
import com.elder.common.core.page.TableDataInfo;

/**
 * 用药记录Controller
 * 
 * @author elder
 * @date 2024-04-11
 */
@RestController
public class MedicationRecordsController extends BaseController
{
    @Autowired
    private IMedicationRecordsService medicationRecordsService;

    /**
     * 查询用药记录列表
     */
    @Anonymous
    @GetMapping("business/medication/list")
    public TableDataInfo list(String elderName, String drugName ,String startTime,String endTime,Integer pageNum,Integer pageSize )
    {
        return medicationRecordsService.getMedicationRecordsList(elderName,drugName,startTime,endTime,pageNum,pageSize);
    }

    /**
     * 导出用药记录列表
     */
    @Anonymous
    @Log(title = "用药记录", businessType = BusinessType.EXPORT)
    @PostMapping("business/medication/export")
    public void export(HttpServletResponse response, MedicationRecords medicationRecords)
    {
        List<MedicationRecords> list = medicationRecordsService.selectMedicationRecordsList(medicationRecords);
        ExcelUtil<MedicationRecords> util = new ExcelUtil<MedicationRecords>(MedicationRecords.class);
        util.exportExcel(response, list, "用药记录数据");
    }

    /**
     * 获取用药记录详细信息
     */
    @Anonymous
    @GetMapping(value = "business/medication/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(medicationRecordsService.selectMedicationRecordsById(id));
    }

    /**
     * 新增用药记录
     */
    @Anonymous
    @Log(title = "用药记录", businessType = BusinessType.INSERT)
    @PostMapping("business/medication")
    public AjaxResult add(@RequestBody MedicationRecordsDTO medicationRecords)
    {
        return toAjax(medicationRecordsService.insertMedicationRecords(medicationRecords));
    }

    /**
     * 修改用药记录
     */
    @Anonymous
    @Log(title = "用药记录", businessType = BusinessType.UPDATE)
    @PutMapping("business/medication")
    public AjaxResult edit(@RequestBody MedicationRecordsDTO medicationRecords)
    {
        return toAjax(medicationRecordsService.updateMedicationRecords(medicationRecords));
    }

    /**
     * 删除用药记录
     */
    @Anonymous
    @Log(title = "用药记录", businessType = BusinessType.DELETE)
	@DeleteMapping("business/medication/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(medicationRecordsService.deleteMedicationRecordsByIds(ids));
    }

    @Anonymous
    @RequestMapping("business/nurse/elderList")
    public TableDataInfo getNurseElderList(Long userId ,String elderName,Integer pageNum,Integer pageSize){
        return medicationRecordsService.getNurseElderList(userId,elderName,pageNum,pageSize);
    }

    @Anonymous
    @RequestMapping("business/elder/medication/finish")
    public AjaxResult nurseMedicationFinish(Long userId,Long elderId,String drugName,String timeBucket){
        return medicationRecordsService.nurseMedicationFinish(userId,elderId,drugName,timeBucket);
    }
}
